package com.marktotop.file.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.marktotop.file.entity.TbUser;
import com.marktotop.file.response.ResultBean;
import com.marktotop.file.service.TbUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * userController类
 *      跨域处理的三种方式：全局配置注册Cors协议 、 单个接口,使用注解@CrossOrigin 、 自定义跨域过滤器
 * @author WXLD
 */
@Slf4j(topic = "user")
@CrossOrigin
@Api(tags = "系统用户接口")
@RestController
@RequestMapping(value = "/api")
public class TbUserController {

    @Autowired
    private TbUserService userService;


    /**
     * 添加用户，使用post请求
     * @param user
     * @return
     */
    @ApiOperation(value = "新增用户",notes="新增用户,post 方式")
    @RequestMapping(value = "/insertUser",method = RequestMethod.POST)
    public Object InsertUser(@RequestBody TbUser user){
        boolean result = userService.insertUser(user);
        if(result == false){
            return ResultBean.error().message("新增用户失败！");
        }
        return ResultBean.ok();
    }


    /**
     * 根据id修改用户信息
     * @param user
     * @return
     */
    @ApiOperation(value = "修改单一用户",notes="根据id修改用户,POST方式")
    @RequestMapping(value = "/updateUser",method = RequestMethod.POST)
    public ResultBean updateUser(@RequestBody TbUser user) {
        Boolean result = userService.updateUser(user);
        if (result == false) {
            return ResultBean.error();
        }
        return ResultBean.ok();
    }


    /**
     * 根据传入的信息查询用户，姓名模糊查询 + 性别 联合查询
     * @param user
     * @return
     */
    @ApiOperation(value = "模糊查询用户",notes="姓名、性别模糊查询用户列表 get 方式")
    @RequestMapping(value = "/queryUser",method = RequestMethod.POST)
    public ResultBean getUserByQuery(@RequestBody TbUser user){
        List<TbUser> byQuery = userService.getByQuery(user);
        if(byQuery != null){
            return ResultBean.ok().data("data",byQuery);
        }
        return ResultBean.error();
    }

    /**
     * 根据输入id查询用户信息
     * @param id
     * @return
     */
    @ApiOperation(value = "查询单一用户",notes="根据id查询用户信息")
    @RequestMapping(value = "/getUser{id}",method = RequestMethod.GET)
    public ResultBean getUserById(@PathVariable("id") Integer id){
        TbUser user = userService.getUserById(id);
        if (user != null){
            return ResultBean.ok().data("user",user);
        }
        return ResultBean.error();
    }


    /**
     * 查询所有用户 - 分页实现
     * @return
     */
    @ApiOperation(value = "查询全部用户",notes="查询所有用户列表 get 方式")
    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
    public ResultBean findAll(@RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum){
        PageHelper.startPage(pageNum,5);
        List<TbUser> tbUsers = userService.getAllUser();
        if(tbUsers != null){
            // 分页支持
            PageInfo<TbUser> UsePage = new PageInfo<TbUser>(tbUsers);
            return ResultBean.ok().data("users",UsePage);
        }
        return ResultBean.error();
    }


    /**
     * 根据id删除某一个用户信息
     * @return
     */
    @ApiOperation(value = "删除单一用户",notes="根据id删除对应用户")
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.GET)
    public ResultBean deleteUser(@PathVariable("id") Integer id){
        Boolean result = userService.deleteUserById(id);
        if(result == false){
            return ResultBean.error().message("删除用户失败，请重试！");
        }
        return ResultBean.ok();
    }
}
